<?php
namespace App\Http\Model;

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

/**
 * 充值黑名单
 */
class RechargeBlack extends Base
{
    protected $table = 'recharge_blacklist';
    protected $finveMinutesBlackTable = 'recharge_five_minutes_blacklist';
    protected $blackRechargeRedisKey = 'PAY:ORDER:COUNT:USER:';
    protected $payOrderFiveMinutesCount = 'PAY:ORDER:FIVE:MINUTES:COUNT:';//记录加入3次未支付的次数
    protected $payOrderFiveMinutes = 'PAY:ORDER:FIVE:MINUTES:';//string 用户未支付3次缓存
    public function __construct(array $attributes = [])
    {
        parent::__construct($this->table, $attributes);
    }

    /**
     * 新增充值黑名单
     */
    public function addRechargeBlack($userId, $content='', $payType=1)
    {
        if(empty($userId)) {
            return false;
        }
        $log = [];
        $log['user_id'] = $userId;
        $log['content'] = $content;
        $log['is_delete'] = 0;
        $log['pay_type'] = $payType;
        $result = DB::table($this->table)->insertGetId($log);

        return $result;
    }

    /**
     * 删除充值黑名单
     */
    public function delRechargeBlack($id)
    {
        if(empty($id)) {
            return false;
        }

        $res = DB::table($this->table)->where('id', $id)->where('is_delete', 0)->update(['is_delete' => 1]);
        $info = DB::table($this->table)->where('id', $id)->first();
        $info = self::objToArray($info);
        //删除redis
        Redis::del($this->blackRechargeRedisKey.$info['user_id'].':PAYTYPE:'.$info['pay_type']);
        Redis::del($this->payOrderFiveMinutes.$info['user_id'].':PAYTYPE:'.$info['pay_type']);
        Redis::del($this->payOrderFiveMinutesCount.$info['user_id'].':PAYTYPE:'.$info['pay_type']);
        return $res;
    }

    /**
     * 获取充值黑名单
     */
    public function getRechargeBlack($userId, $pay_type){
        $info = DB::table($this->table)
            ->where("user_id", $userId)
            ->where('pay_type',$pay_type)
            ->where("is_delete", 0)
            ->first();
        $info = self::objToArray($info);

        return $info;
    }
    
    /**
     * 新增未支付3次订单记录,5分中失效
     */
    public function addRechargeFiveMinutesBlack($userId, $pay_type, $content='未支付订单超过3笔'){
        $log = [];
        $time = time();
        $log['user_id'] = $userId;
        $log['content'] = $content;
        $log['is_delete'] = 0;
        $log['pay_type'] = $pay_type;
        $log['log_time'] = date('Y-m-d H:i:s',$time);
        $log['created_at'] = date('Y-m-d H:i:s',time());
        $result = DB::table($this->finveMinutesBlackTable)->insertGetId($log);

        return $result; 
    }

    /**
     * 查看是否含有临时充值黑名单
     */
    public function getShortRechargeBlack($userId, $pay_type){
        $result = DB::table($this->finveMinutesBlackTable)
                    ->where('user_id',$userId)
                    ->where('pay_type',$pay_type)
                    ->where('is_delete',0)
                    ->first();
        $result = self::objToArray($result);
        return $result;  
    }

    /**
     * 删除临时充值黑名单
     */
    public function shortRechargeBlackDel($id){
        $result = DB::table($this->finveMinutesBlackTable)->where('id',$id)->update(['is_delete'=>1]);
        //删除redis
        $info = DB::table($this->finveMinutesBlackTable)->where('id', $id)->first();
        $info = self::objToArray($info);
        Redis::del($this->blackRechargeRedisKey.$info['user_id'].':PAYTYPE:'.$info['pay_type']);
        Redis::del($this->payOrderFiveMinutes.$info['user_id'].':PAYTYPE:'.$info['pay_type']);
        Redis::del($this->payOrderFiveMinutesCount.$info['user_id'].':PAYTYPE:'.$info['pay_type']);
        return $result;
    }

}
